package com.baturu.vin.byd.service.impl;

import com.baturu.vin.base.service.EPCCacheRealTimeProcess;
import com.baturu.vin.byd.dal.dao.BydQueryResultDAO;
import com.baturu.vin.byd.dto.BydVinInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Slf4j
@Service("bydEPCCacheRealTimeProcess")
public class BydEPCCacheRealTimeProcess implements EPCCacheRealTimeProcess {

    @Autowired
    private BydQueryResultDAO bydQueryResultDAO;

    @Override
    public boolean buildVinAnalyticalResults(String vinCode, String analyticalResults) {
        log.info("vin队列返回的数据:{}", analyticalResults);
        BydVinInfoDTO bydVinInfoDTO = analyseResults(analyticalResults);
        if(bydVinInfoDTO != null) {
            bydQueryResultDAO.createOrUpdateCarInfo(bydVinInfoDTO);
        }
        return true;
    }

    private BydVinInfoDTO analyseResults(String analyticalResults) {
        if(StringUtils.isBlank(analyticalResults)) {
            return null;
        }
        try {
            JSONObject jsonObject = new JSONObject(analyticalResults);
            Set<String> keySet = jsonObject.keySet();
            if(CollectionUtils.isNotEmpty(keySet)) {
                Object vininfoObj = jsonObject.get("vininfo");
                if(vininfoObj == null) {
                    return null;
                }
                JSONObject vinInfo = (JSONObject) vininfoObj;
                Set<String> vinKeySet = vinInfo.keySet();
                if(CollectionUtils.isNotEmpty(vinKeySet)) {
                    Object colorName = vinInfo.get("color");
                    Object engine = vinInfo.get("engine");
                    Object modelName = vinInfo.get("modelName");
                    Object yearName = vinInfo.get("year");
                    Object gearbox = vinInfo.get("gearbox");
                    Object vinNo = vinInfo.get("vinNum");
                    Object seriesName = vinInfo.get("seriesName");

                    Object brandId = vinInfo.get("brandId");
                    Object yearId = vinInfo.get("yearId");
                    Object seriesId = vinInfo.get("seriesId");
                    Object modelId = vinInfo.get("modelId");


                    return BydVinInfoDTO.builder()
                            .carSystem(transObjToStr(seriesName))
                            .year(transObjToInteger(yearName))
                            .vinCode(transObjToStr(vinNo))
                            .carType(transObjToStr(modelName))
                            .engine(transObjToStr(engine))
                            .colorName(transObjToStr(colorName))
                            .gearBox(transObjToStr(gearbox))
                            .brandName("比亚迪")
                            .versionId(transObjToInteger(brandId))
                            .mjId(transObjToInteger(yearId))
                            .carSystemId(transObjToInteger(seriesId))
                            .carTypeId(transObjToInteger(modelId))
                            .build();
                }
            }
        } catch (JSONException e) {
            log.error("传输参数出错",e);
        }
        return null;
    }

    private String transObjToStr(Object obj) {
        if(obj == null || JSONObject.NULL.equals(obj)) {
            return null;
        }
        return obj.toString();
    }

    private Integer transObjToInteger(Object obj) {
        if (obj == null || JSONObject.NULL.equals(obj)) {
            return null;
        }
        return Integer.valueOf(obj.toString());
    }

}
